package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        log.info("添加的收货地址为：{}",addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        //SQL:insert into reggie.address_book(id,user_id,....) value(***,***,.....)
        addressBookService.save(addressBook);
        return R.success("新增成功");
    }

    /**
     * 展示用户所有的收货地址信息
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(currentId != null,AddressBook::getUserId,currentId);
        wrapper.orderByDesc(AddressBook::getIsDefault).orderByDesc(AddressBook::getUpdateTime);

        //SQL:selete * from address_book where user_id = ? oreder by update_time desc
        List<AddressBook> addressBookList = addressBookService.list(wrapper);

        if (addressBookList != null){
            return R.success(addressBookList);
        }else {
            return R.error("暂无收货地址");
        }
    }

    /**
     * 获取最新地址
     * @return
     */
/*    @GetMapping("/lastUpdate")
    public R<AddressBook> getLastUpdate(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AddressBook::getUpdateTime);

        AddressBook LastUpdate = addressBookService.getOne(wrapper);

        return R.success(LastUpdate);
    }*/

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1);
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getOne(wrapper);

        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        log.info("将id为{}的地址设置为默认地址",addressBook.getId());

        //构建条件修改构造器
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault,0);
        //将该用户所有地址都设置普通地址
        // SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(updateWrapper);

        //将当前地址设置为默认地址
        addressBook.setIsDefault(1);

        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);

        return R.success("设置默认地址成功");
    }

    /**
     * 根据id查询地址，用于修改时信息的回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        log.info("要查询的id为：{}" ,id);
        //SQL：selete * from address_book where id = ?
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        log.info("要修改的地址为；{}",addressBook);
        //SQL:update address_book set id = ..,user_id = ..,..... where id = ?
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 删除地址信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("要删除的地址id为：{}",ids);
        //SQL:delete from address_book where ids = ?
        addressBookService.removeById(ids);
        return R.success("删除成功");

    }
}
